/*
 * This file is part of Espruino, a JavaScript interpreter for Microcontrollers
 *
 * Copyright (C) 2020 Gordon Williams <gw@pur3.co.uk>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * ----------------------------------------------------------------------------
 * Machine generated vector font header (by build_vector_font.sh)
 * ----------------------------------------------------------------------------
 */

#ifndef NO_VECTOR_FONT
#include "vector_font.h"

const uint8_t vfFirstChar = 33;
const uint8_t vfLastChar = 255;
#define VF_CHAR_WIDTH 13
#define VF_SCALE 16
#define VF_OFFSET_Y (-2)
#define VF_CHAR_SPACING 1
static const uint8_t vfPolyVerts[] IN_FLASH_MEMORY = {
  169,171,197,195,
  39,41,145,143,
  39,41,93,91,
  42,44,96,94,
  43,45,199,197,
  47,49,203,201,
  130,141,167,156,
  79,90,116,105,
  29,30,212,211,
  42,43,58,85,83,69,68,80,93,107,108,123,150,163,188,199,198,183,156,158,172,173,161,148,134,133,118,91,78,53,
  46,48,197,195,
  40,42,56,82,94,93,65,80,69,93,68,41,69,54,65,41,66,93,92,78,52,
  149,151,165,191,203,202,174,189,178,202,177,150,178,163,174,150,175,202,201,187,161,
  44,55,80,107,203,205,109,82,70,84,96,118,143,156,183,198,200,189,177,127,125,150,174,172,158,145,133,111,86,59,
  29,31,68,106,158,198,239,237,196,156,104,66,
  26,28,69,109,161,199,236,234,197,159,107,67,
  42,44,148,146,
  53,78,137,112,
  59,86,131,104,
  104,112,138,130,
  68,70,174,172,
  170,172,198,223,221,196,
  104,109,135,130,
  33,35,210,208,
  42,44,59,86,84,70,68,80,78,53,
  156,158,172,174,162,164,189,200,198,183,
  78,80,158,156,
  84,86,164,162,
  42,44,174,177,203,195,169,172,68,91,65,
  84,86,99,124,159,184,171,177,203,195,169,144,109,97,
  84,86,99,124,151,164,162,149,135,133,107,109,97,
  44,46,202,200,83,132,138,164,156,130,
  39,47,73,67,106,130,
  94,96,111,138,164,162,136,122,120,132,130,105,
  39,47,73,199,197,71,65,
  78,80,93,107,109,97,84,86,99,136,110,151,164,162,149,135,133,145,158,156,143,106,132,91,
  78,80,106,120,122,110,84,86,164,162,136,137,148,146,131,104,
  91,93,119,117,
  92,94,120,118,
  73,99,121,151,177,117,
  78,86,112,104,
  130,138,164,156,
  65,91,121,143,169,125,
  84,86,99,124,135,148,146,120,108,109,97,
  172,174,200,198,
  69,73,88,102,129,168,192,191,162,150,136,148,162,177,188,173,146,121,110,125,152,166,165,153,140,113,98,96,107,132,158,185,200,205,218,229,225,210,196,169,117,92,80,
  43,45,205,203,83,164,158,133,137,83,197,195,
  39,44,59,86,99,124,110,125,152,165,190,201,197,171,175,163,150,136,132,106,109,97,84,70,67,197,195,
  43,46,61,75,86,71,70,81,106,132,159,174,175,164,179,191,202,199,184,170,143,91,66,54,
  39,44,59,73,100,152,177,189,200,197,171,173,162,137,111,84,69,67,197,195,
  39,47,73,67,106,110,136,132,171,177,203,195,
  39,47,73,67,106,110,136,132,197,195,
  123,127,179,177,151,149,
  39,41,106,111,46,48,204,202,137,132,197,195,
  39,43,69,68,172,173,199,195,169,170,66,65,
  46,48,165,190,201,198,183,156,145,172,158,173,174,163,175,150,
  39,41,119,46,48,121,204,202,133,145,197,195,
  39,41,171,177,203,195,
  39,41,149,148,48,50,206,204,100,175,174,93,197,195,
  39,41,150,46,48,204,202,93,197,195,
  43,46,61,75,102,154,179,191,202,200,174,175,164,139,113,86,71,70,81,106,132,159,174,200,199,184,170,143,91,66,54,
  39,44,59,86,112,137,148,145,119,122,110,84,70,67,197,195,
  177,188,230,219,
  134,136,204,202,
  78,80,93,107,109,124,151,164,162,149,135,133,118,91,
  39,49,75,71,201,199,69,65,
  39,41,158,156,
  39,41,161,47,49,201,199,
  39,41,200,198,44,46,204,202,49,51,204,202,44,46,200,198,
  39,41,205,203,
  47,49,197,195,
  39,41,109,47,49,136,201,199,134,
  39,47,73,171,177,203,195,169,71,65,
  26,31,57,54,210,213,239,234,
  26,28,217,215,
  26,31,239,234,208,211,55,52,
  43,117,119,82,123,125,
  208,216,242,234,
  39,41,82,80,
  96,98,202,200,
  94,95,110,137,163,188,199,173,161,135,121,120,132,158,172,173,199,198,183,156,130,105,
  39,41,197,195,
  94,95,110,137,135,121,120,132,158,172,173,161,163,188,199,198,183,156,130,105,
  44,46,202,200,
  94,95,110,137,163,158,132,135,121,120,132,158,172,174,188,199,198,183,156,130,105,
  43,44,70,69,81,198,196,79,54,
  91,96,122,117,
  96,98,202,227,238,237,222,208,196,211,212,200,
  94,95,110,137,202,200,135,121,120,132,105,
  91,93,197,195,
  39,41,67,65,
  93,95,199,224,235,234,208,209,197,
  41,43,69,67,
  39,41,145,96,98,147,202,200,159,171,197,195,
  99,100,115,142,207,205,140,126,125,137,110,
  91,93,236,234,
  96,98,241,239,
  94,95,110,124,135,121,120,132,105,
  94,95,110,124,136,121,120,132,119,133,134,149,163,188,199,198,183,169,157,172,173,161,174,160,159,144,130,105,
  40,42,159,173,174,200,199,184,157,
  91,93,158,172,173,161,96,98,202,200,187,188,199,198,183,156,
  91,93,160,97,99,200,198,
  91,93,200,198,96,98,204,202,101,103,204,202,149,200,198,
  91,93,203,201,
  97,99,197,195,
  98,100,200,225,236,235,209,210,198,
  91,93,174,199,
  91,98,124,172,176,202,195,169,121,117,
  31,32,58,57,69,95,120,132,146,173,199,213,214,240,239,224,197,171,144,158,143,117,106,118,93,67,42,
  26,28,236,234,
  26,27,42,69,95,122,108,123,149,160,148,173,199,224,235,234,208,209,197,171,146,134,120,93,67,53,52,
  81,92,117,130,132,119,107,121,148,163,152,127,114,112,125,137,123,96,
  44,47,62,87,72,71,82,107,133,160,175,176,165,192,203,200,185,171,144,92,67,55,
  104,112,125,117,
  130,138,151,143,
  65,67,93,91,
  117,119,223,221,
  81,82,97,124,122,108,107,119,145,159,160,148,150,175,186,185,170,143,117,92,
  68,69,199,198,
  43,44,59,73,84,70,69,81,146,171,177,203,195,169,144,79,54,
  104,110,136,130,
  66,78,92,117,144,156,170,158,173,148,146,132,106,94,96,110,136,148,173,162,176,164,150,125,98,86,72,84,69,80,
  118,126,139,131,
  144,152,165,157,
  143,145,236,234,
  26,28,119,117,
  43,45,71,69,
  43,47,62,76,103,155,180,192,203,190,179,154,102,75,60,56,67,92,144,171,186,190,203,199,184,170,143,91,66,54,
  70,72,87,114,112,98,96,108,134,148,150,138,140,165,176,174,159,132,106,81,
  94,108,132,160,172,130,
  97,111,135,163,175,133,
  104,112,177,175,136,130,
  43,47,60,56,67,92,144,171,186,190,203,199,184,170,143,91,66,54,
  47,60,75,102,154,179,190,203,192,180,155,103,76,62,
  68,72,100,126,138,178,176,136,150,148,122,124,98,96,174,172,
  39,47,73,65,
  55,57,161,159,
  169,177,203,195,
  91,99,125,117,
  41,43,80,78,
  156,184,236,234,
  130,132,158,156,
  196,197,211,224,236,234,221,222,210,223,209,
  95,107,135,159,173,137,
  92,104,132,156,170,134,
  47,49,198,196,
  41,42,94,95,108,105,92,93,54,66,53,
  138,139,204,203,138,139,176,179,192,188,175,
  137,139,153,166,190,192,205,202,189,165,152,150,
  40,42,56,69,81,68,82,95,107,105,92,94,81,80,67,68,55,53,
  120,122,148,160,159,171,184,198,200,188,190,215,226,224,209,182,169,144,133,
  68,70,96,94,
  3,5,32,31,
  5,7,31,30,
  4,6,34,32,18,30,28,
  4,15,28,17,32,21,8,19,
  2,4,30,28,
  6,8,34,32,
  4,6,20,46,58,57,29,44,33,57,32,5,33,18,29,5,30,57,56,42,16,
  44,51,77,73,112,115,141,138,177,181,207,201,162,159,134,136,71,46,197,195,
  200,201,215,228,240,238,225,226,214,227,213,
  2,4,31,30,
  4,6,30,29,
  3,5,33,31,17,29,27,
  1,3,29,27,
  5,7,33,31,
  0,2,29,28,
  2,4,28,27,
  40,44,70,69,173,174,200,196,170,171,67,66,
  2,4,32,30,16,28,26,
  0,2,28,26,
  4,6,32,30,
  40,45,60,74,101,153,178,190,201,198,172,174,163,138,112,85,70,68,198,196,
  4,6,33,32,
  66,78,176,164,
  72,86,170,156,
  48,50,197,195,
  39,41,145,156,
  39,41,67,70,85,112,138,163,174,171,145,148,136,110,96,93,197,195,
  42,43,58,85,98,110,123,151,177,189,200,198,172,174,162,149,121,108,96,83,69,68,80,197,195,78,53,
  41,43,70,69,
  42,44,68,67,
  42,44,72,70,56,68,66,
  42,53,66,55,70,59,46,57,
  40,42,68,66,
  44,46,72,70,
  29,31,45,71,83,82,54,69,58,82,57,30,58,43,54,30,55,82,81,67,41,
  94,95,110,99,100,115,142,168,163,137,140,126,125,137,163,177,179,193,204,203,188,199,197,169,156,132,135,161,159,171,158,172,173,161,135,121,119,105,
  198,199,213,226,238,236,223,224,212,225,211,
  92,94,198,196,
  39,41,68,67,
  40,42,66,65,
  93,95,199,197,
  41,43,71,69,55,67,65,
  41,53,68,95,137,124,84,56,
  44,58,93,79,
  146,148,174,172,
  96,98,197,195,
  43,45,69,68,
  39,41,236,234,
  45,47,73,71
};
static const uint8_t vfPolyLengths[] IN_FLASH_MEMORY = {
  4,4,4,4,4,4,4,4,4,30,4,21,21,30,12,12,4,4,4,4,4,6,4,4,10,10,4,4,11,14,13,10,6,12,7,24,16,4,4,6,4,4,6,11,4,43,12,27,24,20,12,10,6,12,12,16,12,6,14,10,31,16,4,4,14,8,4,7,16,4,4,9,10,8,4,8,6,4,4,4,22,4,20,4,21,9,4,12,11,4,4,9,4,12,11,4,4,9,28,9,16,7,15,4,4,9,4,10,27,4,27,18,22,4,4,4,4,20,4,17,4,30,4,4,4,4,4,30,20,6,6,6,18,14,16,4,4,4,4,4,4,4,11,6,6,4,11,11,12,18,19,4,4,4,7,8,4,4,21,20,11,4,4,7,4,4,4,4,12,7,4,4,20,4,4,4,4,4,18,27,4,4,7,8,4,4,21,38,11,4,4,4,4,7,8,4,4,4,4,4,4
};
static const uint8_t vfCharPolys[] IN_FLASH_MEMORY = {
  0,1,
  2,3,
  4,5,6,7,
  8,9,
  10,11,12,
  13,
  2,
  14,
  15,
  16,17,18,
  19,20,
  21,
  22,
  0,
  23,
  24,25,26,27,
  28,
  29,24,
  30,24,25,
  31,
  32,25,33,
  33,25,24,26,
  34,
  35,24,25,
  36,25,24,
  37,0,
  21,38,
  39,
  40,41,
  42,
  43,44,24,
  45,
  46,
  47,
  48,
  49,
  50,
  51,
  52,48,
  53,
  54,
  55,
  56,
  57,
  58,
  59,
  60,
  61,
  62,60,
  63,61,
  64,24,25,
  65,
  66,55,
  67,
  68,
  69,70,
  71,
  72,
  73,
  74,
  75,
  76,
  77,
  78,
  79,80,
  80,81,
  82,
  80,83,
  84,
  85,86,
  87,80,
  88,81,
  89,90,
  91,92,
  93,
  81,
  88,89,94,
  88,89,
  80,
  80,95,
  80,96,
  97,89,
  98,
  99,86,
  100,
  101,
  102,
  103,104,
  105,106,
  107,
  108,
  109,
  110,
  111,
  112,113,114,
  115,116,
  117,118,
  119,120,
  121,
  122,123,71,
  124,125,
  90,126,
  127,128,
  129,130,
  131,
  132,133,134,
  135,
  11,
  136,137,138,
  139,
  140,100,
  141,
  142,
  143,144,
  145,146,147,
  145,148,146,
  145,147,149,
  150,151,
  152,46,
  153,46,
  154,46,
  155,46,
  156,157,46,
  158,46,
  159,
  48,160,
  50,161,
  50,162,
  50,163,
  50,164,165,
  54,166,
  54,167,
  168,169,
  168,170,171,
  172,22,
  59,155,
  173,60,
  153,60,
  154,60,
  155,60,
  156,157,60,
  174,175,
  176,60,
  152,177,55,
  162,177,55,
  154,177,55,
  156,157,177,55,
  71,153,
  178,
  179,
  180,79,80,
  181,79,80,
  182,79,80,
  183,79,80,
  184,185,79,80,
  79,80,186,
  187,
  82,188,
  84,180,
  84,181,
  84,182,
  84,184,185,
  189,190,
  89,191,
  192,193,
  192,90,126,
  80,194,195,
  183,88,89,
  80,180,
  80,181,
  80,182,
  80,183,
  80,184,185,
  19,151,196,
  80,197,
  100,180,
  100,181,
  100,182,
  100,184,185,
  105,106,198,
  199,80,
  105,106,92,200
};
static const uint8_t vfCharLengths[] IN_FLASH_MEMORY = {
  2,2,4,2,3,1,1,1,1,3,2,1,1,1,1,4,1,2,3,1,3,4,1,3,3,2,2,1,2,1,3,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,2,2,3,1,2,1,1,2,1,1,1,1,1,1,1,1,2,2,1,2,1,2,2,2,2,2,1,1,3,2,1,2,2,2,1,2,1,1,1,2,2,1,1,1,1,1,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,1,3,2,0,2,2,0,2,1,0,3,1,1,3,0,0,1,2,0,1,1,0,0,2,3,3,3,2,2,2,2,2,3,2,1,2,2,2,2,3,2,2,2,3,2,2,2,2,2,2,3,2,2,3,3,3,4,2,1,1,3,3,3,3,4,3,1,2,2,2,2,3,2,2,2,3,3,3,2,2,2,2,3,3,2,2,2,2,3,3,2,4
};

static const uint8_t *vfGetPolyPtr(unsigned char index, int *polyLen) {
  if (index >= sizeof(vfPolyLengths)) return NULL;

  *polyLen = vfPolyLengths[index];
  const uint8_t *ptr = vfPolyVerts;
  for (unsigned char i=0; i < index; ++i) {
    ptr += vfPolyLengths[i];
  }
  return ptr;
}

static const uint8_t *vfGetCharPtr(char sch, int *charLen) {
  unsigned char ch = (unsigned char)sch;
  if (ch<vfFirstChar || ch>vfLastChar) return NULL;
  ch = (unsigned char)(ch - vfFirstChar);

  const uint8_t *ptr = vfCharPolys;
  for (unsigned char i=0; i < ch; ++i) {
    ptr += vfCharLengths[i];
  }
  *charLen = vfCharLengths[ch];
  return ptr;
}

// prints character, returns width
unsigned int vfGetCharFromPtr(graphicsPolyCallback callback, void *callbackData, int x1, int y1, int sizex, int sizey, const uint8_t *charPtr, int charLen) {
  x1 = (x1<<4) - 8;
  y1 = (y1<<4) - 8;
  int w = 0;
  for (int i = 0; i < charLen; ++i) {
    short poly[86];
    int polyLen;
    const uint8_t *p = vfGetPolyPtr(charPtr[i], &polyLen);
    for (int j = 0; j < polyLen; ++j) {
      uint8_t vertex = p[j];
      int vx = vertex % VF_CHAR_WIDTH;
      int vy = vertex / VF_CHAR_WIDTH;
      if (vx>w) w=vx;
      poly[j*2  ] = (short)(x1 + vx*sizex*16/VF_SCALE);
      poly[j*2+1] = (short)(y1 + (vy+VF_OFFSET_Y)*sizey*16/VF_SCALE);
    }
    callback(callbackData, polyLen, poly);
  }
  return (unsigned int)(((w+1+VF_CHAR_SPACING)*sizex*16/VF_SCALE+7)>>4);
}

// returns the width of a character
unsigned int graphicsVectorCharWidth(unsigned int sizex, char ch) {
  int charLen;
  const uint8_t *charPtr = vfGetCharPtr(ch, &charLen);
  if (!charPtr) return (unsigned int)(sizex/2); // space
  int w = 0;
  for (int i = 0; i < charLen; ++i) {
    int polyLen;
    const uint8_t *p = vfGetPolyPtr(charPtr[i], &polyLen);
    for (int j = 0; j < polyLen; ++j) {
      uint8_t vertex = p[j];
      int vx = vertex % VF_CHAR_WIDTH;
      if (vx>w) w=vx;
    }
  }
  return ((unsigned int)(w+1+VF_CHAR_SPACING)*sizex*16/VF_SCALE+7)>>4;
}

// prints character, returns width
unsigned int graphicsGetVectorChar(graphicsPolyCallback callback, void *callbackData, int x1, int y1, int sizex, int sizey, char ch) {
  int charLen;
  const uint8_t *charPtr = vfGetCharPtr(ch, &charLen);
  if (!charPtr) return (unsigned int)(sizex/2); // space
  return vfGetCharFromPtr(callback, callbackData, x1, y1, sizex, sizey, charPtr, charLen);
}

#endif
